<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <link href="css/login.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="login">
    <div class="message">后台管理系统</div>
    <div id="darkbannerwrap"></div>

    <form id="login-form" method="post" onsubmit="return false;">
        <input id="username" name="username" placeholder="用户名" type="text" autocomplete="off">
        <hr class="hr15">
        <input id="password" name="password" placeholder="密码" type="password" autocomplete="off">
        <hr class="hr15">

        <button style="width: 100%;" type="submit" onclick="login(this)">登录</button>
        <hr class="hr15">

        <a href="login-sms.html">短信登录</a>
        <span id="info" style="color: red;"></span>
    </form>
</div>
</body>

<script src="js/constant.js"></script>
<script src="js/libs/jquery-3.3.1.min.js"></script>
<script type="text/javascript">

    if (top != self) {
      parent.location.href=loginPage;
    }

    var access_token = localStorage.getItem("access_token");
    if(access_token !=null && access_token.trim().length!=0){
        $.ajax({
            type : 'get',
            url : domainName + '/api-u/users/current?access_token=' + access_token,
            success : function(data) {
                location.href = 'index.html';
            },
            error : function(xhr, textStatus, errorThrown) {
                if (xhr.status == 401) {
                    localStorage.removeItem("access_token");
                }
            }
        });
    }

    /**
     * 登录方法
     */
    function login(obj){
        $(obj).attr("disabled", true);

        var username = $.trim($("#username").val());
        var password = $.trim($("#password").val());
        if(username=="" || password==""){
            $("#info").html('用户名或者密码不能为空');
            $(obj).attr("disabled", false);
        }else{
            $.ajax({
                type : 'post',
                url : domainName + '/sys/login',
                timeout : 5000, //超时时间设置，单位毫秒
                data : $("#login-form").serialize(),
                success : function(data) {
                    //将access_token和refresh_token写入本地
                    localStorage.setItem("access_token", data.access_token);
                    localStorage.setItem("refresh_token", data.refresh_token);
                    location.href = 'index.html';
                },
                error : function(xhr, textStatus, errorThrown) {
                    $(obj).attr("disabled", false);
                    if(textStatus == 'timeout') {
                        $("#info").html("登陆超时,请重试");
                        return;
                    }
                    var msg = xhr.responseText;
                    if(msg == undefined){
                        $("#info").html("请重试");
                    } else {
                        var response = JSON.parse(msg);
                        var message = response.error_description;
                        if(message == undefined){
                            message = response.message;
                        }
                        $("#info").html(message);
                    }
                }
            });
        }
    }
</script>
</html>